<template>
  <div class="lesson-create-edit">
    <el-form label-position="right" label-width="100px">
      <el-form-item label="课程名称">
        <el-input v-model="courseName" disabled></el-input>
      </el-form-item>
      <el-form-item label="章节名称" >
        <el-input v-model="sectionName" disabled></el-input>
      </el-form-item>
      <el-form-item label="课时名称">
        <el-input v-model="lessonForm.theme"></el-input>
      </el-form-item>
      <el-form-item label="时长">
        <el-input type="number" v-model="lessonForm.duration">
          <template slot="append">分钟</template>
        </el-input>
      </el-form-item>
      <el-form-item label="是否开放试听">
          <el-switch
            v-model="lessonForm.isFree"
            active-color="#40586f"
            inactive-color="#dcdfe6">
          </el-switch>
      </el-form-item>
      <el-form-item label="课时排序">
        <el-input type="Number" v-model="lessonForm.orderNum">
          <template slot="append">数字控制排序，数字越大越靠后</template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">确定</el-button>
        <el-button @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getCourseById } from '@/services/course'
import { getBySectionId, saveOrUpdateLesson, getById } from '@/services/course-section'

export default {
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    sectionId: {
      type: [Number, String]
    },
    courseId: {
      type: [Number, String]
    },
    lessonId: {
      type: [Number, String]
    }
  },
  name: 'LessonCreateEdit',
  data () {
    return {
      dialogVisible1: true,
      lessonForm: {
        courseId: this.courseId,
        sectionId: this.sectionId,
        theme: '',
        duration: 0,
        isFree: false,
        orderNum: 0
      },
      courseName: '',
      sectionName: ''
    }
  },
  created () {
    if (this.isEdit) {
      // 加载课时信息
      this.loadLessonInfo()
    }
    this.laodCourseName()
    this.laodSectionName()
  },
  methods: {
    async laodCourseName () {
      const { data } = await getCourseById(this.courseId)
      if (data.code === '000000') {
        this.courseName = data.data.courseName
      }
    },
    async laodSectionName () {
      const { data } = await getBySectionId(this.sectionId)
      if (data.code === '000000') {
        this.sectionName = data.data.sectionName
      }
    },
    async loadLessonInfo () {
      const { data } = await getById(this.lessonId)
      if (data.code === '000000') {
        this.lessonForm = data.data
      }
    },
    async onSubmit () {
      const { data } = await saveOrUpdateLesson(this.lessonForm)
      if (data.code === '000000') {
        console.log(data)
        this.$message.success(`${this.isEdit ? '编辑' : '添加'}成功`)
      }
      // 关闭提示框(需要子组件向父组件传递状态)
      this.$emit('success')
    },
    onCancel () {
      // 设置取消状态，让父组件处理
      this.$emit('cancel')
      this.lessonForm = {}
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-form {
    width: 90%;
    margin: 0 auto;
  }
</style>
